<?php
namespace Admin\Controller;
use Vendor\Tree;

class LuckdrawController extends ComController
{
    public function index()
    {
        // $config = D('Sysparams')->getBytype(1);
        $config = M('Lucky_draw')->where(['id'=>1])->find();

        $this->assign('config', $config);

        return $this->display();
    }

    public function saveparams(){
        $data = I('param.');

        if (!isset($data['name']) || empty($data['name'])) {
            return $this->ajaxReturn(array('msg'=>'参数错误','status'=>0));
        }

        $res = D('Sysparams')->saveByKey($data['name'], $data['value']);

        if (!$res) {
            return $this->ajaxReturn(array('msg'=>'修改失败','status'=>0));
        }

        return $this->ajaxReturn(array('msg'=>'操作成功','status'=>1));
    }

    public function record(){
        $p        = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $offset   = $pagesize * ($p - 1);
        $pagesize = 20;#每页数量
        $order    = I('get.order','DESC');
        $m        = M('Lucky_results');

        $field = '';

        if ($order == 'asc') {
            $order = "add_time asc";
        } elseif (($order == 'desc')) {
            $order = "add_time desc";
        } else {
            $order = "id desc";
        }

        $keyword = I('keyword') ? I('keyword') : '';

        $map = array(
            'status' => 1,
        );

        if (!empty($keyword)) {
            $map['user_name'] = array('like', "$keyword%");
            $this->assign('keyword', $keyword);
        }

        $list = $m->alias('r')
        ->join('LEFT JOIN __USERS__ u ON u.user_id=r.uid')
        ->field('r.*,u.user_name')
        ->where($map)->page($p, $pagesize)->order($order)->select();

        $count = $m->alias('r')
        ->join('LEFT JOIN __USERS__ u ON u.user_id=r.uid')
        ->where($map)->count();

        $page = new \Think\Page($count, $pagesize);

        $page = $page->show();

        $this->assign('list', $list);
        $this->assign('page', $page);

        return $this->display();
    }

    public function goods(){
        $p        = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $offset   = $pagesize * ($p - 1);
        $pagesize = 20;
        $order    = I('get.order','DESC');

        $m        = M('Lucky_prize');

        $field = '';

        if ($order == 'asc') {
            $order = "add_time asc";
        } elseif (($order == 'desc')) {
            $order = "add_time desc";
        } else {
            $order = "id desc";
        }

        $keyword = I('keyword') ? I('keyword') : '';

        $map = array(
            'status' => 1,
        );

        if (!empty($keyword)) {
            $map['prize'] = array('like', "$keyword%");
            $this->assign('keyword', $keyword);
        }

        $list = $m->where($map)->page($p, $pagesize)->order($order)->select();

        $count = $m->where($map)->count();

        $page = new \Think\Page($count, $pagesize);

        $page = $page->show();

        $this->assign('list', $list);
        $this->assign('page', $page);

        return $this->display();
    }

    public function setting(){

        if (!IS_POST) {
            return $this->error('请求失败 -1');
        }

        $m = D('LuckyDraw');

        if (!$m->create()) {
            return $this->error($m->getError());
        }

        if (false === $m->save()) {
            return $this->error($m->getError());
        }

        addlog('修改抽奖设置');
        return $this->success('设置成功');
    }

    public function editGoods(){
        $id = I('id',0,'intval');

        if (empty($id)) {
            return $this->error('参数错误');
        }

        if (!IS_POST) {
            $info = M('Lucky_prize')->where(['id'=>$id])->find();

            if (empty($info)) {
                return $this->error('获取错误');
            }

            $this->assign('info', $info);
            $this->getLuckDraw($info['draw_id']);
            return $this->display();
        }

        $data = I('post.');

        $needParams = array(
            'drawsid' => 'notEmpty',
            'prize'   => 'notEmtpy',
            'odds'    => 'notEmpty',
            'number'  => 'notEmpty',
        );

        if (!checkForm($needParams, $data)) {
            return $this->error('参数错误');;
        }

        // $data['draw_id'] = $data['drawsid'];
        unset($data['drawsid']);

        $res = M('Lucky_prize')->where(['id'=>$id])->save($data);

        if (false === $res) {
            return $this->error('修改失败');
        }

        addlog('修改抽奖礼品');
        return $this->success('修改成功');
    }

    public function delGoods($id){
        if (empty($id)) {
            return $this->error('参数错误');
        }

        $res = M('Lucky_prize')->data(['status'=>-1])->where(['id'=>intval($id)])->save();

        if (false == $res) {
            return $this->error('参数错误');
        }

        addlog('删除抽奖礼品');
        return $this->success('删除成功');
    }

    public function addGoods(){
        if (!IS_POST) {
            $this->getLuckDraw();
            return $this->display();
        }

        $data = I('post.');

        $needParams = array(
            'drawsid' => 'notEmpty',
            'prize'   => 'notEmtpy',
            'odds'    => 'notEmpty',
            'number'  => 'notEmpty',
        );

        if (!checkForm($needParams, $data)) {
            return $this->error('参数错误');
        }

        if ($data['oods'] > 90) {
            return $this->error('中奖几率太高');
        }

        $data['add_time']   = time();
        $data['remain_num'] = $data['number'];
        $data['draw_id']    = $data['drawsid'];

        unset($data['drawsid']);

        $res = M('Lucky_prize')->data($data)->add();

        if (!$res) {
            return $this->error('添加失败!');
        }

        addlog('添加抽奖礼品');
        return $this->success('添加成功');
    }

    public function getLuckDraw($sid = 0)
    {
       $draws = M('Lucky_draw')->where(['status'=>1])->field('id,title')->order('id asc')->select();
       $tree = new Tree($draws);
       $str = "<option value=\$id \$selected>\$title</option>";
       $draws = $tree->get_tree(0, $str, $sid);

       $this->assign('luckDraws', $draws);
   }
}
